package com.yztc.yidoutang.activity.result_activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.yztc.yidoutang.R;
import com.yztc.yidoutang.adapter.ViewPagerAdapter;
import com.yztc.yidoutang.base.BaseActivity;
import com.yztc.yidoutang.fragment.danpin_fragment.DanPinFragment;
import com.yztc.yidoutang.fragment.darenshuo_fragment.DaRenShuoFragment;
import com.yztc.yidoutang.fragment.pic_fragment.PicFragment;
import com.yztc.yidoutang.fragment.quanwuji_fragment.QuanWuJiFragment;
import com.yztc.yidoutang.share.SharedURL;

import java.util.ArrayList;
import java.util.List;

public class ResultActivity extends BaseActivity {
    private String[] titles={"图片","全屋记","单品","达人说"};
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initEvent();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_result;
    }
    public void initView(){
        Intent intent=getIntent();
        String queryWords=intent.getStringExtra("keywords");
        int selectionId=intent.getIntExtra("selectionid", SharedURL.SOUTUPIAN_ID);
        Log.i("tag","----"+queryWords);
        mTabLayout= (TabLayout) findViewById(R.id.result_tab);
        mViewPager= (ViewPager) findViewById(R.id.result_vp);
        for (int i=0;i<titles.length;i++){
            mTabLayout.addTab(mTabLayout.newTab().setText(titles[i]));
        }
        List<Fragment> fragmentList=new ArrayList<>();
        Bundle bundle=new Bundle();
        bundle.putString("queryWords",queryWords);

        PicFragment picFragment=new PicFragment();
        picFragment.setArguments(bundle);

        QuanWuJiFragment quanWuJiFragment=new QuanWuJiFragment();
        quanWuJiFragment.setArguments(bundle);

        DanPinFragment danPinFragment=new DanPinFragment();
        danPinFragment.setArguments(bundle);

        DaRenShuoFragment daRenShuoFragment=new DaRenShuoFragment();
        daRenShuoFragment.setArguments(bundle);

        fragmentList.add(picFragment);
        fragmentList.add(quanWuJiFragment);
        fragmentList.add(danPinFragment);
        fragmentList.add(daRenShuoFragment);

        ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager(),fragmentList);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(selectionId);
    }

    public void initEvent(){
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

}
